//app.js
var httpUtil = require('./utils/httpUtil.js');
const Towxml = require('/towxml/main');     //引入towxml库

App({
  towxml: new Towxml(),
  onLaunch: function () { 
    var userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo);
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    wx.checkSession({
      success() {
         if (userInfo == null || userInfo==""){
          wx.login({
            success(res) {
              if (res.code) {
                httpUtil.getHttp("/user/getOpenId", { code: res.code }, function (res) {
                  console.log('登录成功' + res.openId)
                   wx.setStorageSync('userInfo', res)
                });
              } else {
                console.log('登录失败！' + res.errMsg)
              }
            }
          })
        }
      },
      fail() {
        // session_key 已经失效，需要重新执行登录流程
        wx.login({
          success(res) {
            if (res.code) {
              httpUtil.getHttp("/user/getOpenId", { code: res.code }, function (res) {
                console.log('登录成功' + res.openId)
                 wx.setStorageSync('userInfo', res)
              });
            } else {
              console.log('登录失败！' + res.errMsg)
            }
          }
        })
      }
    })
  },
  globalData: {
    appKey: "wx8902cbbbe202d0d6",
    appSecre: "77fee8751a0056ede02f353454d59d94"
  }
})